
package com.iot.manage.biz.service.impl;

import java.util.List;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.iot.common.core.util.R;
import com.iot.manage.api.domain.SysMenu;
import com.iot.manage.api.domain.SysOrg;
import com.iot.manage.api.domain.SysRole;
import com.iot.manage.api.domain.SysRoleMenu;
import com.iot.manage.api.domain.SysUser;
import com.iot.manage.api.domain.SysUserOrg;
import com.iot.manage.api.domain.SysUserRole;
import com.iot.manage.biz.service.SysUserService;
import com.jfinal.kit.StrKit;

import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {
	
	@Setter
	private Throwable cause;

	@Override
	public R<SysUser> info(String id) throws Exception {
		SysUser sysUser = SysUser.dao.findById(id);
		if (sysUser == null) {
			throw new Exception("未查询到用户信息！");
		}
		sysUser.setPassword("");
		List<SysRole> roles = SysRole.dao.find("SELECT * FROM " + SysRole.TABLE_NAME
				+ " WHERE ID IN (SELECT ROLE_ID FROM " + SysUserRole.TABLE_NAME + " WHERE USER_ID=? ) ",
				sysUser.getId());
		sysUser.setRoles(roles);
		List<SysMenu> permissions = SysMenu.dao.find("SELECT A.* FROM " + SysMenu.TABLE_NAME + " A,"
				+ SysRoleMenu.TABLE_NAME + " B " + " WHERE A.ID=B.MENU_ID and B.ROLE_ID IN (SELECT ROLE_ID FROM "
				+ SysUserRole.TABLE_NAME + " WHERE USER_ID=? ) AND A.ENABLED='1' and  A.TYPE='1' ", sysUser.getId());
		sysUser.setPermissions(permissions);
		return R.ok(sysUser);
	}

	public R<SysUser> loadUserByUsername(String username) {

		SysUser sysUser = SysUser.dao.findByUsername(username);
		if (sysUser == null) {
			throw new UsernameNotFoundException("用户【" + username + "】不存在.");
		}
		List<SysRole> roles = SysRole.dao.find("SELECT * FROM " + SysRole.TABLE_NAME
				+ " WHERE ID IN (SELECT USER_ID FROM " + SysUserRole.TABLE_NAME + " WHERE USER_ID=? ) ",
				sysUser.getId());
		sysUser.setRoles(roles);

		List<SysMenu> permissions = SysMenu.dao.find("SELECT A.* FROM " + SysMenu.TABLE_NAME + " A,"
				+ SysRoleMenu.TABLE_NAME + " B " + " WHERE A.ID=B.MENU_ID and B.ROLE_ID IN (SELECT ROLE_ID FROM "
				+ SysUserRole.TABLE_NAME + " WHERE USER_ID=? ) AND A.ENABLED='1' ", sysUser.getId());
		sysUser.setPermissions(permissions);
		return R.ok(sysUser);
	}

	public R<List<SysMenu>> getUserMenus(String userId, String type) throws Exception {
		return R.ok(getMenus(userId, type));
	}

	public List<SysMenu> getMenus(String userId, String type) {
		String _sql = "SELECT A.* FROM " + SysMenu.TABLE_NAME + " A," + SysRoleMenu.TABLE_NAME + " B "
				+ " WHERE A.ID=B.MENU_ID and B.ROLE_ID IN (SELECT ROLE_ID FROM " + SysUserRole.TABLE_NAME
				+ " WHERE USER_ID=? ) and A.parentId='-1' AND A.ENABLED='1' ";
		if (!StrKit.isBlank(type)) {
			_sql += " AND A.TYPE='" + type + "' ";
		}
		_sql += " ORDER BY A.SORT ASC ";
		List<SysMenu> children = SysMenu.dao.find(_sql, userId);
		for (SysMenu menu : children) {
			getChildren(menu, userId, type);
		}
		return children;
	}

	public void getChildren(SysMenu parent, String userId, String type) {

		String _sql = "SELECT A.* FROM " + SysMenu.TABLE_NAME + " A," + SysRoleMenu.TABLE_NAME + " B "
				+ " WHERE A.ID=B.MENU_ID and B.ROLE_ID IN (SELECT ROLE_ID FROM " + SysUserRole.TABLE_NAME
				+ " WHERE USER_ID=? ) and A.PARENTID=?  AND A.ENABLED='1' ";
		if (!StrKit.isBlank(type)) {
			_sql += " AND A.TYPE='" + type + "' ";
		}
		_sql += " ORDER BY A.SORT ASC ";
		List<SysMenu> children = SysMenu.dao.find(_sql, userId, parent.getId());
		for (SysMenu menu : children) {
			getChildren(menu, userId, type);
		}
		parent.setChildren(children);
	}

	public List<SysOrg> getUserOrgs(String userId) {
		List<SysOrg> children = SysOrg.dao.find("SELECT A.*,(CASE  WHEN (SELECT COUNT(ORGID) AS COUNT FROM "
				+ SysUserOrg.TABLE_NAME + " WHERE ORGID=A.ID AND USERID=?)>0 THEN 1 ELSE  0 END )  as checked FROM "
				+ SysOrg.TABLE_NAME + " A WHERE A.PARENTID='-1'   ORDER BY A.SORT ASC ", userId);
		for (SysOrg menu : children) {
			getOrgChildren(menu, userId);
		}
		return children;
	}

	public void getOrgChildren(SysOrg parent, String userId) {
		List<SysOrg> children = SysOrg.dao.find("SELECT A.*,(CASE  WHEN (SELECT COUNT(ORGID) AS COUNT FROM "
				+ SysUserOrg.TABLE_NAME + " WHERE ORGID=A.ID AND USERID=?)>0 THEN 1 ELSE  0 END )  as checked FROM "
				+ SysOrg.TABLE_NAME + " A WHERE A.PARENTID=?   ORDER BY A.SORT ASC ", userId, parent.getId());
		for (SysOrg org : children) {
			getOrgChildren(org, userId);
		}
		parent.setChildren(children);
	}

}
